﻿Imports System.Data.SqlClient
Public Class frmRecoverEquips

    Dim isRecover As Boolean = False
    Private Sub frmRecoverEquips_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        numLocation = 0
        cnClose()
    End Sub

    Private Sub frmRecoverEquips_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = Frm_Main
        open()

        'remplir combo personelles
        Dim htClient As New Hashtable()
        htClient.Clear()
        Cmd.CommandText = "select * from Personnels where [categorie] like 'employé'"
        Dr = Cmd.ExecuteReader()
        Do While Dr.Read()
            htClient.Add(Dr.GetInt32(0), Dr.GetString(1) & " " & Dr.GetString(2))
        Loop
        Dr.Close()

        For Each ImageType As DictionaryEntry In htClient
            cmbPerson.Items.Add(ImageType)
        Next
        cmbPerson.DisplayMember = "value"
        cmbPerson.ValueMember = "key"
        If cmbPerson.Items.Count > 0 Then
            cmbPerson.SelectedIndex = 0
        End If

        If numLocation > 0 Then
            Cmd.CommandText = "select tlte.idType,te.categorie,tte.libelle,tlte.quantite,tl.dateLivraison,tre.recepteur,ep.quantite,ep.prixUniRembour " &
"from trEquipement te inner join trTypeEquipement tte " &
                                "on te.id = tte.idEquipement " &
                                "inner join trLocTypeEquip tlte on tlte.idType = tte.idType " &
                                "inner join trLocation tl on tl.numLocation = tlte.numLoc " &
                                "left join trRetourEquipLoue tre on tre.numLocation = tlte.numLoc " &
                                "left join equipementPerdu ep on tlte.idType = ep.idType " &
                                "where tlte.numLoc = " & numLocation

            Dr = Cmd.ExecuteReader
            Dim i As Integer = 0
            While Dr.Read()
                If i <= 0 Then
                    If Not Dr(5) Is DBNull.Value Then
                        For Each it As DictionaryEntry In cmbPerson.Items
                            If it.Key = Dr.GetInt32(5) Then
                                cmbPerson.SelectedItem = it
                                Exit For
                            End If
                        Next
                        isRecover = True
                    End If
                End If
                txtNumBon.Text = numLocation
                lstVEquipLoue.Items.Add(New ListViewItem(New String() {Dr.GetInt32(0), Dr.GetString(1), Dr.GetString(2), Dr.GetInt32(3), convertNullToString(Dr(6), "0"), convertNullToString(Dr(7), "0")}))
                i += 1
            End While
            Dr.Close()
        End If
    End Sub


    Private Sub btn_ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter.Click
        Dim message As String = "Ajout"
        If cmbPerson.SelectedIndex >= 0 Then
            If Not isRecover Then
                Cmd.CommandText = "INSERT INTO [Salle_Mariage].[dbo].[trRetourEquipLoue] values(" & numLocation & "," & CType(cmbPerson.SelectedItem, DictionaryEntry).Key & ")"
                Cmd.ExecuteNonQuery()
            Else
                Cmd.CommandText = "delete from equipementPerdu where numLoc = " & numLocation
                Cmd.ExecuteNonQuery()
                message = "modification"
            End If
            Cmd.Parameters.Clear()
            For Each it As ListViewItem In lstVEquipLoue.Items
                If it.SubItems(4).Text <> "0" And it.SubItems(5).Text <> "0" Then
                    Cmd.Parameters.Add("@prixUniRembour", SqlDbType.Float)
                    Cmd.CommandText = "INSERT INTO [equipementPerdu] VALUES(" & it.SubItems(0).Text & "," & txtNumBon.Text & "," & it.SubItems(4).Text & ",@prixUniRembour)"
                    Cmd.Parameters(0).Value = CDbl(it.SubItems(5).Text)
                    Cmd.ExecuteNonQuery()

                    Cmd.Parameters.Clear()
                    Cmd.CommandText = "update trTypeEquipement set quantiteStock -= " & it.SubItems(4).Text & " where idType = " & it.SubItems(0).Text
                    Cmd.ExecuteNonQuery()
                End If
            Next
            MsgBox(message & " de demande réussi", MsgBoxStyle.Information, "Succés")
            frmLstLocation.Show()
            Me.Close()
        End If
    End Sub

    Private Sub btnAddAdj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAdj.Click
        If lstVEquipLoue.SelectedItems.Count > 0 Then
            If Not String.IsNullOrEmpty(txtQuantPerdu.Text) And Not String.IsNullOrWhiteSpace(txtQuantPerdu.Text) _
                And Not String.IsNullOrEmpty(txtPrixUnit.Text) And Not String.IsNullOrWhiteSpace(txtPrixUnit.Text) Then
                If CInt(txtQuantPerdu.Text) <= CInt(lstVEquipLoue.SelectedItems(0).SubItems(3).Text) And txtQuantPerdu.Text <> "0" Then
                    lstVEquipLoue.SelectedItems(0).SubItems(4).Text = txtQuantPerdu.Text
                    lstVEquipLoue.SelectedItems(0).SubItems(5).Text = txtPrixUnit.Text
                Else
                    MsgBox("Entrez une valeur inférieur ou égale la quantité loué")
                End If
            Else
                MsgBox("Veuillez remplir les champs")
            End If
        Else
            MsgBox("Veuillez séléctionner un équipement")
        End If
    End Sub

    Private Sub txtQuantPerdu_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuantPerdu.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPrixUnit_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrixUnit.KeyPress
        Dim allowedChars As String = "1234567890,"
        If txtPrixUnit.Text.Contains(",") Then
            allowedChars = "1234567890"
        End If
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub
End Class